<template>
  <div>
    <div class="title hover-hand">
      <h2>最新音乐</h2>
    </div>

    <div class="main">
      <div
        class="item"
        v-for="(item, ind) in newSongList"
        :key="item.id"
        @dblclick="playMusic(item.id, ind)"
      >
        <div class="number yahei">
          <!-- 这里用++ind会影响ind自身数值，后面用到ind会报错 -->
          <h4 v-show="playingId != ind">{{ ind + 1 }}</h4>
          <i class="iconfont yahei" v-show="playingId === ind">&#xe7a5;</i>
        </div>
        <div class="cover hover-hand">
          <img :src="item.picUrl" alt="" />
        </div>
        <div class="information">
          <div class="song-name yahei hover-hand">{{ item.name }}</div>
          <div class="author yahei hover-hand">
            {{ handleAuthor(item.song.artists) }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { handleAuthor } from "utils/author.js";
import bus from "utils/bus.js";
export default {
  name: "newsong",
  props: {
    newSongList: Array,
  },
  data() {
    return {
      playingId: -1,
    };
  },
  watch: {
    playingId(newValue) {
      bus.$emit("musicPlaying", newValue);
    },
  },
  created() {
    bus.$on("ended", (ind) => {
      this.playingId = ind;
    });
  },
  methods: {
    // 获取作者名，有可能多个作者
    // artists(i){
    //   //报错要学会排查
    //   // console.log(i);
    //   // console.log(this.newSongList[i]);
    //   if(this.newSongList[i].song.artists.length > 1) {
    //     let author = ''
    //     for(let index = 0;index < this.newSongList[i].song.artists.length;index++) {
    //       author += this.newSongList[i].song.artists[index].name + '/'
    //     }
    //     return author.slice(0,author.length-1)
    //   } else{
    //     return this.newSongList[i].song.artists[0].name
    //   }
    // }
    handleAuthor,
    playMusic(id, ind) {
      bus.$emit("play", id, this.newSongList, ind);
      this.playingId = ind;
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  padding-bottom: 0.058333rem;
  border-bottom: 1px solid #b8b6b6;
}
.main {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    margin-top: 0.083333rem;
    margin-bottom: 0.083333rem;
    width: 50%;
    height: 0.625rem;
    .number {
      height: 100%;
      width: 0.358333rem;
      text-align: center;
      line-height: 0.625rem;
      i {
        color: lightseagreen;
      }
    }
    .cover {
      width: 0.45rem;
      height: 0.45rem;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .information {
      position: relative;
      flex: 1;
      margin-left: 0.083333rem;
      height: 0.45rem;
      .author {
        position: absolute;
        bottom: 0;
      }
    }
  }
}
</style>
